<?php
namespace app\Manage\controller;

use app\common\controller\Manage;
use app\common\model\Balance as BalanceModel;
use app\common\model\GoodsComment as GoodsCommentModel;
use app\common\model\UserGrade as UserGradeModel;
use app\common\model\UserLog as UserLogModel;
use app\common\model\User as UserModel;
use app\common\model\UserWx as UserWxModel;
use app\common\model\UserPointLog as UserPointLogModel;
use app\common\model\ManageUserRole as ManageUserRoleModel;
use app\common\model\ManageUserRoleRel as ManageUserRoleRelModel;
use app\common\model\WxDailyRetain as WxDailyRetainModel;
use app\common\model\Position as PositionModel;
use think\facade\Request; 

class User extends Manage
{
    public function index()
    {
        if(Request::isAjax()){
            $userModel = new UserModel();
            return $userModel->tableData(input('param.'));
        }else{
            return $this->fetch('index');
        }
    }
 

    //取当前店铺的所有用户的登录退出消息,现在是绑定死一个用户，以后可能有多个用户
    public function userLogList()
    {
        $userLogModel = new UserLogModel();
        return $userLogModel->getList(session('user.id'));
    }

    //用户统计
    public function statistics()
    {
        $WxDailyRetainModel = new WxDailyRetainModel();
        $list_uv = $WxDailyRetainModel->statistics(7);

        $data = [
            'legend' => [
                'data' => ['新增记录', '活跃记录']
            ],
            'xAxis' => [
                [
                    'type' => 'category',
                    'data' => $list_uv['day']
                ]
            ],
            'series' => [
                [
                    'name' => '新增记录',
                    'type' => 'line',
                    'data' => $list_uv['uvNew']
                ],
                [
                    'name' => '活跃记录',
                    'type' => 'line',
                    'data' => $list_uv['uv']
                ]
            ]
        ];
        return $data;
    }

  
    /**
     * 添加用户
     * @return array|mixed
     */
    public function addUser()
    {
        //$this->view->engine->layout(false);
        if (Request::isPost()) {
            $input     = Request::param();
            $userModel = new UserModel();

            // 判断是否有同名的角色 
            $data['mobile'] = $input['mobile'];
            $data = $userModel->tableData($data);
  
            if( $data['count'] > 0) { 
                return [
                    'status' => false,
                    'data' => '',
                    'msg' => '已存在相同手机号！'
                ];
            }
 
            $result    = $userModel->manageAdd($input);
            return $result;
        }
        $gradeModel = new UserGradeModel();
        $userGrade =$gradeModel->getAll();

        $manageRoleModel = new ManageUserRoleModel();
        $manageRoleList = $manageRoleModel->select();

        // 判断默认等级
        $arrlength=count($userGrade);

        for($i=0;$i<$arrlength;$i++) { 
            if($userGrade[$i]['is_def'] == UserGradeModel::IS_DEF_YES){
                $this->assign('defGrade',$userGrade[$i]['name']);
            } 
        }
 
        $positionModel = new PositionModel();
        $posList = $positionModel->select();

        $this->assign('pid', input('param.')['pid']);
        $this->assign('grade', $userGrade);
        $this->assign('roleList', $manageRoleList);
        $this->assign('posList', $posList);
        return $this->fetch('addUser');
    }

    /**
     * 编辑用户
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editUser()
    {
        //$this->view->engine->layout(false);
        $userModel = new UserModel();

        if (Request::isPost()) {
            $input  = Request::param();
            $result = $userModel->manageEdit($input);
            
            // 这里需要清理缓存
            if(cache('?user_operation_'.input('param.id'))){
                cache('user_operation_'.input('param.id'), null);
            }

            return $result;
        }
 
        $manageRoleModel = new ManageUserRoleModel();
        $manageRoleList = $manageRoleModel->select();
        $manageRoleRelModel = new ManageUserRoleRelModel();
        $smList = $manageRoleRelModel->where(['manage_id'=>input('param.user_id')])->select();
        foreach($manageRoleList as $k => $v){
            $checked = false;
            foreach($smList as $i => $j){
                if($j['role_id'] == $v['id']){
                    $checked = true;
                    break;
                }
            }
            $manageRoleList[$k]['checked'] = $checked;
        }

        $positionModel = new PositionModel();
        $posList = $positionModel->select();
        $user_id = Request::param('user_id');
        $info    = $userModel->getUserInfo($user_id);
        $gradeModel = new UserGradeModel();
        $userGrade =$gradeModel->getAll();

        // 获取设备参数
        $positionModel = new PositionModel();
        $positionName = $positionModel->getNameById($info['positionId']);
        $info['positionName'] = $positionName;

        $this->assign('info', $info);
        $this->assign('posList', $posList);
        $this->assign('grade',$userGrade);
        $this->assign('roleList',$manageRoleList);
        return $this->fetch('editUser');
    }


    /**
     * 导入用户
     * @return array|mixed
     */
    public function importUser()
    {
        if (Request::isPost()) {
            $input     = Request::param();
            $userModel = new UserModel();
            $result    = $userModel->manageAdd($input);
            return $result;
        }
        return $this->fetch('importUser');
    }

    /**
     * 编辑余额
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editMoney()
    {
        $this->view->engine->layout(false);
        $user_id = input('user_id');
        $flag = input('flag', 'false');

        if($flag == 'true')
        {
            $money = input('money');
            $balanceMoney = new BalanceModel();
            $res = $balanceMoney->change($user_id, $balanceMoney::TYPE_ADMIN, $money, 0);
            return $res;
        }
        else
        {
            $this->assign('user_id', $user_id);
            $User = new UserModel();
            $where[] = ['id', 'eq', $user_id];
            $user_info = $User->where($where)->find();
            $this->assign('money', $user_info['balance']);
            return $this->fetch('editMoney');
        }
    }

    /**
     * 编辑赠送余额
     * @return array|mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function editGiftMoney()
    {
        $this->view->engine->layout(false);
        $user_id = input('user_id');
        $flag = input('flag', 'false');

        if($flag == 'true')
        {
            $giftmoney = input('giftmoney');
            $balanceMoney = new BalanceModel();
            $res = $balanceMoney->change($user_id, $balanceMoney::TYPE_ADMIN, 0, 0, 0, $giftmoney);
            return $res;
        }
        else
        {
            $this->assign('user_id', $user_id);
            $User = new UserModel();
            $where[] = ['id', 'eq', $user_id];
            $user_info = $User->where($where)->find();
            $this->assign('giftmoney', $user_info['bindingbalance']);
            return $this->fetch('editGiftMoney');
        }
    }
    
    //用户等级删除
    public function unbind(){
        $userWxModel = new UserWxModel();
        if(!input('?param.user_id')){
            return error_code(10000);
        }

        return $userWxModel->unbind(input('param.user_id'));
    }
}
